import axios from 'axios'
import constant from './constant'
import '@/assets/lib/bridge/bridge.js'
import { json2 } from './decrypt'
import { getDataStateHandle } from './decorators'

const remoteDAO = {
  invoke (interfaceStr, params) {
    return new Promise((resolve, reject) => {
      axios({
        method: 'post',
        url: constant.baseURL + interfaceStr,
        data: params
      })
        .then(result => {
          resolve(result.data);
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  @getDataStateHandle
  getUserCode () {
    return new Promise(function (resolve, reject) {
      Bridge.callByJS({
        callback: function (response) {
          if (response.error == '0') {
            var result = json2(response.data);
            var object = JSON.parse(result);
            response.data = object;
            console.log('12000004-获取当前用户-data:' + object.body[0].USER_CODE)
            resolve(object.body[0].USER_CODE)
          } else {
            reject(response.data)
          }
        },
        'id': 'h5',
        'tranCode': 'UC12000004',
        'jsToken': '12000004',
        'isEncrypt': 'false',
        'transmitMode': 'get',
        'param': {
          'OP_SITE': '127.0.0.1',
          'OP_PROGRAM': '',
          'BRANCH_NO': '0',
          'TOKEN_SERIAL_NO': '0',
          'USER_TOKEN': '',
          'func_id': '12000004',
          'CUST_CODE': '',
          'USER_CODE': '0',
          'PASSWORD': '',
          'terminal_source': '02' // 必须加
        },
        'localParameter': {'mobile': 'ACCT_CODE,phone_no,mobile', 'password': 'stock_pwd'}
      })
    })
  },

  /* 用户行为操作记录
   * @param userCode
   * @param actionFlag 操作标志 A:增加  U：更新  D：删除
   * @param acctType 身份类型 0-一户通账号 1-资金账号 2-身份证号   4-QQ号  5-客户代码  6：注册手机号  7：股东账号   8：基金账号
   * @param acctCode 对应的身份号码
   * @param behaviorFlag 行为标志 L:login（登录）  QD:签到  HY:活跃  TG:偷看  NGQD：牛股签到
   * @param sysSource 系统来源
   *                  00：一户通终端 01：网上商城 02：易阳指  03：富易  04：小额贷款  05：百事通
   *                  06：组合大本营  07：国泰君安官网 08：网上营业厅 10：新富易  11：渠道推广 12：i理财
   * @param remark 备注
   * @param callback
   */
  @getDataStateHandle
  callUC12000022 (userCode) {
    return new Promise((resolve, reject) => {
      Bridge.callByJS({
        callback: function (response) {
          if (response.error == '1') {
            console.log('12000022-用户行为操作记录-error:', response)
            reject(response)
          } else {
            var result = json2(response.data);
            var json = JSON.parse(result);
            response.data = json;
            console.log('12000022-用户行为操作记录-data:', response);
            resolve(response)
          }
        },
        'id': 'h5',
        'tranCode': 'UC12000022',
        'jsToken': 'UC12000022',
        'isEncrypt': 'false',
        'transmitMode': 'get',
        'param': {
          'FUNC_ID': '12000022',
          'OP_BRANCH_NO': '0',
          'OP_SITE': '127.0.0.1',
          'OP_PROGRAM': '',
          'BRANCH_NO': '0',
          'PASSWORD': '',
          // 'USER_TOKEN': '',
          'TOKEN_SERIAL_NO': '2016000000000011003000050020000000000000000000000000000000000000001212126868000000000000000000000000',
          'USER_CODE': userCode + '',
          'ACTION_FLAG': 'A',
          'ACCT_TYPE': '6',
          // 'ACCT_CODE': acctCode,
          'BEHAVIOR_FLAG': 'NGQD',
          'SYS_SOURCE': '02',
          'REMARK': '',
          'ACTIVITY_CODE': '201709_QDHD'
        },
        'localParameter': {'mobile': 'ACCT_CODE,phone_no,mobile', 'password': 'stock_pwd,USER_TOKEN'}
      })
    })
  },

  // 获取签到记录
  @getDataStateHandle
  callUC12000023 (userCode, beginDate, endDate) {
    return new Promise(function (resolve, reject) {
      Bridge.callByJS({
        callback: function (response) {
          if (response.error == 1) {
            console.log('12000023获取签到记录-error:', response);
            reject(response)
          } else {
            var result = json2(response.data);
            var object = JSON.parse(result);
            response.data = object;
            // console.log('12000023获取签到记录-data:', response)
            resolve(response.data)
          }
        },
        'id': 'h5',
        'tranCode': 'UC12000023',
        'jsToken': 'UC12000023',
        'isEncrypt': 'false',
        'transmitMode': 'get',
        'param': {
          'FUNC_ID': '12000023',
          'OP_BRANCH_NO': '0',
          'OP_SITE': '127.0.0.1',
          'OP_PROGRAM': '',
          'BRANCH_NO': '0',
          'PASSWORD': '',
          'USER_CODE': userCode + '',
          'BEGIN_DATE': beginDate + '',
          'BEHAVIOR_FLAG': 'NGQD',
          'OP_CODE': '8888',
          'OP_WAY': '0',
          'SYS_SOURCE': '02',
          'BEHAVIOR_DATE': '',
          'END_DATE': endDate + '',
          // 'USER_TOKEN': '',
          'TOKEN_SERIAL_NO': '2016000000000011003000050020000000000000000000000000000000000000001212126868000000000000000000000000'
        },
        'localParameter': {'mobile': 'ACCT_CODE,phone_no,mobile', 'password': 'stock_pwd,USER_TOKEN'}
      })
    })
  },

  // 用户抽奖
  @getDataStateHandle
  callUC12000304 (userCode) {
    return new Promise(function (resolve, reject) {
      Bridge.callByJS({
        callback: function (response) {
          if (response.error == 1) {
            console.log('12000304用户抽奖-error:', response);
            reject(response)
          } else {
            var result = json2(response.data);
            var object = JSON.parse(result);
            response.data = object;
            console.log('12000304用户抽奖-data:', response)
            resolve(response)
          }
        },
        'id': 'h5',
        'tranCode': 'UC12000304',
        'jsToken': 'UC12000304',
        'isEncrypt': 'false',
        'transmitMode': 'get',
        'param': {
          'FUNC_ID': '12000304',
          'OP_BRANCH_NO': '0',
          'OP_SITE': '127.0.0.1',
          'OP_CODE': '8888',
          'OP_WAY': '0',
          'ACTIVITY_CODE': '201709_QDHD',
          'POOL_ID': '',
          'USER_CODE': userCode + '',
          'OP_PROGRAM': '',
          'BRANCH_NO': '0',
          'PASSWORD': '',
          // 'USER_TOKEN': '',
          'TOKEN_SERIAL_NO': '2016000000000011003000050020000000000000000000000000000000000000001212126868000000000000000000000000'
        },
        'localParameter': {'mobile': 'ACCT_CODE,phone_no,mobile', 'password': 'stock_pwd,USER_TOKEN'}
      })
    })
  },

  // 礼品次数查询
  @getDataStateHandle
  callUC12000330 (userCode) {
    return new Promise((resolve, reject) => {
      Bridge.callByJS({
        callback: function (response) {
          if (response.error == '1') {
            console.log('12000330-礼品次数查询-error:', response)
            reject(response)
          } else {
            var result = json2(response.data);
            var json = JSON.parse(result);
            response.data = json;
            // console.log('12000330-礼品次数查询-data:', response);
            resolve(response)
          }
        },
        'id': 'h5',
        'tranCode': 'UC12000330',
        'jsToken': 'UC12000330',
        'isEncrypt': 'false',
        'transmitMode': 'get',
        'param': {
          'FUNC_ID': '12000330',
          'OP_BRANCH_NO': '0',
          'OP_SITE': '127.0.0.1',
          'OP_PROGRAM': '',
          'OP_CODE': '8888',
          'OP_WAY': '0',
          'BRANCH_NO': '0',
          'PASSWORD': '',
          // 'USER_TOKEN': '',
          'TOKEN_SERIAL_NO': '2016000000000011003000050020000000000000000000000000000000000000001212126868000000000000000000000000',
          'USER_CODE': userCode,
          'ACTIVITY_CODE': '201709_QDHD',
          'BAG_TYPE': 'DRAW'
        },
        'localParameter': {'mobile': 'ACCT_CODE,phone_no,mobile', 'password': 'stock_pwd,USER_TOKEN'}
      })
    })
  }
}

export default remoteDAO

